using System;

namespace MT5Trade.Models
{
    /// <summary>
    /// 对冲仓位对数据结构，用于组合盈亏策略
    /// </summary>
    public class HedgePair
    {
        /// <summary>
        /// 主账号订单票号
        /// </summary>
        public ulong MainTicket { get; set; }

        /// <summary>
        /// 从账号订单票号
        /// </summary>
        public ulong FollowTicket { get; set; }

        /// <summary>
        /// 交易品种（主账号）
        /// </summary>
        public string MainSymbol { get; set; }

        /// <summary>
        /// 交易品种（从账号，可能经过映射）
        /// </summary>
        public string FollowSymbol { get; set; }

        /// <summary>
        /// 主账号交易量（手数）
        /// </summary>
        public double MainVolume { get; set; }

        /// <summary>
        /// 从账号交易量（手数）
        /// </summary>
        public double FollowVolume { get; set; }

        /// <summary>
        /// 主账号开仓时间
        /// </summary>
        public DateTime MainOpenTime { get; set; }

        /// <summary>
        /// 从账号开仓时间
        /// </summary>
        public DateTime FollowOpenTime { get; set; }

        /// <summary>
        /// 主账号交易方向（0=买入，1=卖出）
        /// </summary>
        public int MainType { get; set; }

        /// <summary>
        /// 从账号交易方向（0=买入，1=卖出）
        /// </summary>
        public int FollowType { get; set; }

        /// <summary>
        /// 对冲对创建时间
        /// </summary>
        public DateTime PairCreateTime { get; set; }

        /// <summary>
        /// 最后检查时间
        /// </summary>
        public DateTime LastCheckTime { get; set; }

        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; }

        /// <summary>
        /// 是否活跃
        /// </summary>
        public bool IsActive { get; set; }

        /// <summary>
        /// 历史平均滑点（点数）
        /// </summary>
        public double AverageSlippage { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public HedgePair()
        {
            PairCreateTime = DateTime.Now;
            LastCheckTime = DateTime.Now;
            IsActive = true;
            RetryCount = 0;
            AverageSlippage = 0.0;
        }

        /// <summary>
        /// 检查是否为有效的对冲对（反向交易）
        /// </summary>
        public bool IsValidHedge()
        {
            return MainType != FollowType;
        }

        /// <summary>
        /// 获取对冲对的唯一标识
        /// </summary>
        public string GetPairId()
        {
            return $"{MainTicket}_{FollowTicket}";
        }

        /// <summary>
        /// 计算开仓时间差（秒）
        /// </summary>
        public double GetOpenTimeDifference()
        {
            return Math.Abs((MainOpenTime - FollowOpenTime).TotalSeconds);
        }

        /// <summary>
        /// 更新最后检查时间
        /// </summary>
        public void UpdateCheckTime()
        {
            LastCheckTime = DateTime.Now;
        }

        /// <summary>
        /// 增加重试次数
        /// </summary>
        public void IncrementRetryCount()
        {
            RetryCount++;
        }

        public override string ToString()
        {
            return $"HedgePair[{GetPairId()}]: {MainSymbol}/{FollowSymbol}, Vol:{MainVolume}/{FollowVolume}, Type:{MainType}/{FollowType}";
        }
    }
}